/**
 * 三种Bean注册方式（加载到IOC容器中）：通常业务类使用注解，第三方类或公用Bean使用XML方式（或javaConfig）
 * 1.xml文件配置（Spring 1.x时代）
 * 1）ApplicationContext：在初始化应用上下文时就实例化所有单实例的Bean
 * a)ClassPathXmlApplicationContext:缺省资源类型classpath
 * b)FileSystemXmlApplicationContext:缺省资源类型file,可以用绝对路径（带盘符）或相对路径（项目目录）为根目录
 * c)XmlWebApplicationContext：web程序专用,xml进行配置
 * ServletAC包含RootAC，RootAC是ServletAC的Parent，先在RootAC中搜索，然后在ServletAC中搜索bean
 * WebApplicationContext servletAC = RequestContextUtils.findWebApplicationContext(request);//推荐，
 * Spring容器：WebApplicationContext rootAC = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
 * <p>
 * ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); //在获取失败时抛出异常
 * ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc);//在获取失败时返回null
 * c.1.servlet.getServletContext()
 * c.2.this.getServletContext()
 * c.3.request.getSession().getServletContext();
 * d)AnnotationConfigWebApplicationContext:web程序专用,注解进行配置
 * 2）BeanFactory:在初始化容器时，并未实例化Bean，直到第一次访问某个Bean时才实例化目标Bean
 *
 * 2.注解（Spring 2.x 时代）：xml和注解中两种扫描方式，可用AnnotationConfigApplicationContext来加载
 *
 * 3.JavaConfig（Spring 3.x 时代）：可用AnnotationConfigApplicationContext来加载
 */
package com.example.demospringbootbean;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class BeanLoadDemo {
    public static void showLoad() {
        //XML加载
        ApplicationContext contextA =
                new ClassPathXmlApplicationContext("beans1.xml"); //缺省资源类型是classpath
        /*不同路径XML加载
        ApplicationContext contextB =
                new FileSystemXmlApplicationContext("classpath:beans1.xml"); //缺省资源类型是file,文件路径为：demospringbean/build/resources/main/beans1.xml*/
        BeanA beanA = (BeanA) contextA.getBean("beanA");
        beanA.getMessage();

        //注解和Java Config加载
        AnnotationConfigApplicationContext contextB = new AnnotationConfigApplicationContext(BeanConfiguration.class);
        BeanB beanB = (BeanB) contextB.getBean("beanB");
        beanB.getMessage();

        BeanC beanc = (BeanC) contextB.getBean("beanC");
        beanc.getMessage();

        //使用BeanFactory注册
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        Resource res = new ClassPathResource("beans1.xml");
        reader.loadBeanDefinitions(res);
        /*等效替代：
         reader.loadBeanDefinitions("beans1.xml");*/

        //使用BeanFactory加载
        //可选：BeanFactory beanfactory = (BeanFactory)factory;
        BeanA beanA2 = (BeanA) factory.getBean("beanA");
        beanA2.getMessage();

        //使用ApplicationContextAware加载，会启用当前有效的ApplicationContext
        BeanB beanB2 = (BeanB) AppContextUtil.getBean("beanB");
        /*等效替代
          ApplicationContext ctx=AppContextUtil.getApplicationContext();
          beanA = (BeanA) ctx.getBean("beanA");
         */
        beanB2.getMessage();

        contextB.close();
    }
}
